package com.mxm.mirai.tools;

import com.mxm.mirai.bot.*;
import com.mxm.mirai.config.MXM_config;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.BotFactoryJvm;
import net.mamoe.mirai.console.plugins.PluginBase;
import net.mamoe.mirai.message.*;
import net.mamoe.mirai.network.LoginFailedException;
import net.mamoe.mirai.network.UnsupportedSMSLoginException;
import net.mamoe.mirai.qqandroid.network.protocol.packet.login.WtLogin;
import net.mamoe.mirai.utils.*;

public class BotLogin extends PluginBase {

    public static void main(String[] args) {
        System.out.println(new BotLogin().Run("123456","123456"));
    }
    /**
     * 登录Bot
     * @param qq
     * @param passwrod
     * @return
     */
    public String Run(String qq, String passwrod) {
        if(MXM_config.bottools.inBotOnline(qq))return "请勿重复登录！";//判断登录是否存在
        Bot bot = null;
        try {
            bot = BotFactoryJvm.newBot(new Long(qq), passwrod, new BotConfiguration() {
                {
//                setProtocol(MiraiProtocol.ANDROID_PHONE);
                //保存设备信息到文件
                fileBasedDeviceInfo("deviceInfo.json");

            }
        });
        bot.login();
//        System.out.println("登录日志:"+bot.getLogger().getIdentity());
//        System.out.println("====================输出好友====================");
//        bot.getFriends().forEach(friend -> System.out.println(friend.getId() + ":" + friend.getNick()));
//        System.out.println("====================输出群表====================");
//        bot.getGroups().forEach(group ->
//                System.out.println(group.getId() + ":" + group.getName()+":"+group.getOwner().getNameCard())
//        );
        if (MXM_config.state){
            System.out.println("==================执行到Bot==================");
            getEventListener().subscribeAlways(FriendMessageEvent.class, new BotFriend());//绑定好友消息
            getEventListener().subscribeAlways(GroupMessageEvent.class, new BotElse());
            getEventListener().subscribeAlways(GroupMessageEvent.class, new BotPoem());
            getEventListener().subscribeAlways(GroupMessageEvent.class, new BotPoke());
            getEventListener().subscribeAlways(GroupMessageEvent.class, new BotVideo());
            getEventListener().subscribeAlways(GroupMessageEvent.class, new BotMusic163());
            getEventListener().subscribeAlways(GroupMessageEvent.class, new BotBaiDu());
            getEventListener().subscribeAlways(GroupMessageEvent.class, new BotSmartMatting());
            getEventListener().subscribeAlways(GroupMessageEvent.class, new BotCreateCards());
            MXM_config.state = false;
        }
        System.out.println("==================[登录成功]==================");
            return "登录成功！";
        }catch (UnsupportedSMSLoginException u){
            return u.getMessage();
        }catch (LoginFailedException l){
            bot.close(null);
            bot = null;
            System.out.println("==================执行到B==================");
            return l.getMessage();
        } finally {
            if (bot!=null) {
//                bot.join(); // 阻塞当前线程直到 bot 离线
//                Bot finalBot = bot;
//                new Thread(){
//                    @Override
//                    public void run() {
//                        System.out.println("==================执行到C==================");
//                        finalBot.join(); // 阻塞当前线程直到 bot 离线
//                    }
//                }.start();
            }
        }
    }
}
